﻿	# https://docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting-deployments.html#troubleshooting-deployments-powershell
if ($PSHOME -like "*SysWOW64*")
{
  Write-Warning "Restarting this script under 64-bit Windows PowerShell."

  # Restart this script under 64-bit Windows PowerShell.
  #   (\SysNative\ redirects to \System32\ for 64-bit mode)

  & (Join-Path ($PSHOME -replace "SysWOW64", "SysNative") powershell.exe) -File `
    (Join-Path $PSScriptRoot $MyInvocation.MyCommand) @args

  # Exit 32-bit script.

  Exit $LastExitCode
}

Write-Warning "Hello from $PSHOME"
Write-Warning "  (\SysWOW64\ = 32-bit mode, \System32\ = 64-bit mode)"
Write-Warning "Original arguments (if any): $args"
function ExistsFirewallRule($dispName){
  if(Get-NetFirewallRule | where{$_.DisplayName  -eq $dispName}){
      return [bool]$true
   }else{
      return [bool]$false
   }
}

Import-Module WebAdministration


if(!(Test-Path (-Join("IIS:\AppPools\","wpc_wpaycore_WEBAPIcore_WEBAPI")))){
     New-WebAppPool -Name "wpc_wpaycore_WEBAPI" -Force
  }
  Set-ItemProperty -Path (-Join("IIS:\AppPools\","wpc_wpaycore_WEBAPI")) -Name managedRuntimeVersion -Value '' 

  if(!(Test-Path  ("IIS:\Sites\wpc_wpaycore_WEBAPI"))){
	 New-Item iis:\Sites\wpc_wpaycore_WEBAPI -bindings @{protocol="http";bindingInformation="*:8004:"} -physicalPath C:\UN_WpayCore\WPay.Web.API
  }

  New-WebBinding -Name "wpc_wpaycore_WEBAPI" -IPAddress "*" -Port 80 -HostHeader api.wpccore.wpayapi.weixin.wuerp.com

  Set-ItemProperty -Path (-Join("IIS:\Sites\","wpc_wpaycore_WEBAPI")) -name applicationPool -value "wpc_wpaycore_WEBAPI"

if(!(Test-Path (-Join("IIS:\AppPools\","wpc_wpaycore_PayMachineAPI")))){
     New-WebAppPool -Name "wpc_wpaycore_PayMachineAPI" -Force
  }
  Set-ItemProperty -Path (-Join("IIS:\AppPools\","wpc_wpaycore_PayMachineAPI")) -Name managedRuntimeVersion -Value '' 

  if(!(Test-Path  ("IIS:\Sites\wpc_wpaycore_PayMachineAPI"))){
	 New-Item iis:\Sites\wpc_wpaycore_PayMachineAPI -bindings @{protocol="http";bindingInformation="*:8005:"} -physicalPath C:\UN_WpayCore\WPay.PayMachine.API
  }

  New-WebBinding -Name "wpc_wpaycore_PayMachineAPI" -IPAddress "*" -Port 80 -HostHeader api.wpccore.paymachineapi.weixin.wuerp.com

  Set-ItemProperty -Path (-Join("IIS:\Sites\","wpc_wpaycore_PayMachineAPI")) -name applicationPool -value "wpc_wpaycore_PayMachineAPI"


$firewallRulName='open8002,8003'
$firewallRulDisplayName='open:8002,8003'
$firewallPort='8002,8003'
if(ExistsFirewallRule -dispName $firewallRulDisplayName){
    Remove-NetFirewallRule -DisplayName $firewallRulDisplayName
}
New-NetFirewallRule -Name $firewallRulName -Direction Inbound -DisplayName $firewallRulDisplayName -LocalPort $firewallPort -Protocol 'TCP'  